package com.bitejiuyeke.biteportalservice.homepage.service.impl;

import com.bitejiuyeke.biteadminapi.map.domain.vo.RegionVO;
import com.bitejiuyeke.biteadminapi.map.feign.MapFeignClient;
import com.bitejiuyeke.bitecommoncore.utils.JsonUtil;
import com.bitejiuyeke.bitecommondomain.domain.R;
import com.bitejiuyeke.bitecommondomain.domain.ResultCode;
import com.bitejiuyeke.bitecommonredis.service.RedisService;
import com.bitejiuyeke.biteportalservice.homepage.domain.dto.CityDescDTO;
import com.bitejiuyeke.biteportalservice.homepage.domain.vo.CityDescVO;
import com.bitejiuyeke.biteportalservice.homepage.service.IRegionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
@Slf4j
public class RegionServiceImpl implements IRegionService {

    private static final String REGION_CHILDREN_PREFIX = "applet:region:children:";
    private static final Long REGION_CHILDREN_TIMEOUT = 24 * 60L;

    @Autowired
    private MapFeignClient mapFeignClient;

    @Autowired
    private RedisService redisService;

    @Override
    public List<CityDescDTO> regionChildren(Long parentId) {
        if(null == parentId){
            log.error("区域id为空，无法查询子区域");
        }
        //查缓存
        List<CityDescDTO> regionList = getCacheRegionList(parentId);
        if(!CollectionUtils.isEmpty(regionList)){
            return regionList;
        }
        //如若不存在，远程调用feign，缓存
        R<List<RegionVO>> r = mapFeignClient.regionChildren(parentId);
        if(null == r || r.getCode() != ResultCode.SUCCESS.getCode() || null == r.getData()){
            log.error("获取区域子区域列表失败,parent:{}", parentId);
            return Arrays.asList();
        }

        regionList = r.getData()
                .stream()
                .map(region -> {
                    CityDescDTO cityDescDTO = new CityDescDTO();
                    cityDescDTO.setId(region.getId());
                    cityDescDTO.setName(region.getName());
                    cityDescDTO.setFullName(region.getFullName());
                    return cityDescDTO;
                }).collect(Collectors.toList());
        //缓存
        cacheRegionList(parentId, regionList);

        return regionList;
    }

    private void cacheRegionList(Long parentId, List<CityDescDTO> regionList) {
        if(null == parentId){
            return ;
        }
        redisService.setCacheObject(
                REGION_CHILDREN_PREFIX + parentId,
                JsonUtil.obj2String(regionList),
                REGION_CHILDREN_TIMEOUT,
                TimeUnit.SECONDS
        );
    }

    private List<CityDescDTO> getCacheRegionList(Long parentId) {
        String str = redisService.getCacheObject(REGION_CHILDREN_PREFIX + parentId, String.class);
        if(StringUtils.isBlank(str)){
            return Arrays.asList();
        }
        return JsonUtil.string2Obj(str, List.class);
    }
}
